package com.simon.hotfix;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.simon.hotfix.permission.PermissionFaild;
import com.simon.hotfix.permission.PermissionHelper;
import com.simon.hotfix.permission.PermissionSucceed;

/**
 * dx --dex --no-strict --output=F:\classes2.dex F:\com
 * <p>
 * dx --dex --output=E:\Patch-Dex\classes_fix.dex E:\Patch-Dex
 * <p>
 * 热更新：可新增/修改代码逻辑、修改资源id
 * 不可增加activity、res资源文件(布局/图片/Manifest)、lib、assets
 */
@SuppressWarnings("all")
public class MainActivity extends AppCompatActivity {

    private TextView tv_content;
    private ImageView iv_icon;
    private Button bt_intent;

    private static final int PERMISSION_REQUEST_CODE = 10;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PermissionHelper.requestPermission(this, PERMISSION_REQUEST_CODE, new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE});

        tv_content = findViewById(R.id.tv_content);
        iv_icon = findViewById(R.id.iv_icon);
        bt_intent = findViewById(R.id.bt_intent);

        // error
        tv_content.setText("错误展示");
        iv_icon.setImageResource(R.mipmap.ic_error);

        // right
//        tv_content.setText("正确展示");
//        iv_icon.setImageResource(R.mipmap.ic_launcher);

        bt_intent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @PermissionSucceed(requestCode = PERMISSION_REQUEST_CODE)
    public void requestPermissionsSuccess() {
        Toast.makeText(this, "储存权限开启", Toast.LENGTH_SHORT).show();
    }

    @PermissionFaild(requestCode = PERMISSION_REQUEST_CODE)
    public void requestPermissionsFaild() {
        Toast.makeText(this, "储存权限关闭", Toast.LENGTH_SHORT).show();

        PermissionHelper.requestPermission(this, PERMISSION_REQUEST_CODE, new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE});
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionHelper.requestPermissionsResult(this, requestCode, permissions);
    }

}
